<?php

class TopicController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        session_write_close();
        $model = new Topic('search');
        if (isset($_GET['Topic'])) {
            $model->attributes = $_GET['Topic'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }

    public function actionDelete()
    {
        if (Yii::app()->request->isAjaxRequest) {
            $result = 'Success';
            $id = $this->getQuery('get', 'id', null);
            $model = Topic::model()->findByPk($id);
            if ($model) {
                if ($model->delete()) {
                    TopicExt::model()->deleteAllByAttributes(['tid' => $model->id]);
                    SeriesTopic::model()->deleteAllByAttributes(['tid' => $model->id]);
                    $result = 'Success';
                } else {
                    $result = 'delete failure';
                }
            }
            echo $result;
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }

    }

    public function actionCreate()
    {
        $model = new Topic();
        if (isset($_POST['Topic'])) {
            $model->attributes = $_POST['Topic'];
            if ($model->save()) {
                Yii::app()->user->setFlash('alert-success', 'Create Success');
                $this->redirect($this->defaultAction);
            } else {
                Yii::app()->user->setFlash('alert-failure', 'Create failed');
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }

    public function actionUpdate()
    {
        if (isset($_GET['id'])) {
            $model = Topic::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Topic']) && ($model->attributes = $_POST['Topic'])) {
                    if(!isset($_POST['Topic']['sids']))
                    {
                        $model->sids = [];
                    }
                    if ($model->update()) {
                        Yii::app()->user->setFlash('alert-success', 'Update Success');
                        $this->redirect(array($this->defaultAction));
                    } else {
                        if ($model->hasErrors('status')) {
                            Yii::app()->user->setFlash('alert-failure', 'Update failure , the config can\'t work  and  error is ' . $model->getError('status'));
                        }
                    }
                }
                $this->render('model', array(
                    'model' => $model,
                ));
                return;
            }
        }
        Yii::app()->user->setFlash('alert-failure', "Update failure");
        $this->redirect(array($this->defaultAction));
    }

    public function actionSort()
    {
        if (isset($_GET['id'])) {
            $model = Topic::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Topic']['exts'])) {
                    $data = ($_POST['Topic']['exts']);
                    if ($model->sort($data)) {
                        echo 'Success';
                    } else {
                        echo 'Failed';
                    }
                    return;
                }
                $this->render('sort', array(
                    'model' => $model,
                ));
                return;
            }
        }
        Yii::app()->user->setFlash('alert-failure', "sort failure");
        $this->redirect(array($this->defaultAction));
    }

    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'thumbnail':
                $html = '';
                $html .= '<img class="topic-img" src="%s">';
                $html = vsprintf($html, [$data->url,]);
                return $html;
                break;
            case 'link':
                $html = '';
                $serieses = $data->serieses;
                foreach ($serieses as $seriese) {
                    $html .= '<div><span style="color: ' . $seriese->color . '"> ' . $seriese->name . '</span></div>';
                }
                return $html;
                break;
            case 'operations':
                $params = [];
                $res = '';
                //edit
                $params[] = Yii::app()->getController()->createUrl("update", array("id" => $data->id,));
                $res .= '<a class ="" title="编辑" href="%s"> 编辑</a>';
                //delete
                $params[] = '删除';
                $params[] = Yii::app()->getController()->createUrl("delete", array("id" => $data->id,));
                $res .= '<a class ="delete "  tooltip="delete" title="%s"  href="%s"> 删除</a>';
                $params[] = Yii::app()->getController()->createUrl("topicext/manage", array("id" => $data->id,));
                $res .= '<a class=" " aria-hidden="true" href="%s"> 关联视频</a>';
                $params[] = Yii::app()->getController()->createUrl("sort", array("id" => $data->id,));
                $res .= '<a class="" aria-hidden="true" href="%s"> 排序</a>';
                return vsprintf($res, $params);
                break;
            default:
                return $data->$name;
        }
    }


}